use cmsis_rtos2_rs::static_mem::{Task, Thread};
use utils::log;


use crate::driver;

struct AppTask;

static THREAD: Thread<2048> = Thread::uninit();

#[cfg(feature = "base_app")]
pub mod handler_task;
#[cfg(feature = "base_app")]
pub mod status_task;

pub unsafe fn start() {
    THREAD.spawn(&AppTask);
}


impl Task for AppTask {
    fn run(&self) {
        unsafe { driver::init(); }

        log!("app task run-new");
        
        #[cfg(feature = "bootloader")]
        cfw_kit::ota::run_ota();

        #[cfg(feature = "base_app")]
        unsafe { run_app(); }
    }
}

#[cfg(feature = "base_app")]
unsafe fn run_app() {
    crate::conn::init();

    #[cfg(feature = "cp_drop")]
    crate::cp_drop::init();

    #[cfg(feature = "cp_cargo")]
    crate::cp_cargo::init();

    #[cfg(feature = "cp_door")]
    crate::cp_door::init();

    #[cfg(feature = "cp_heat")]
    crate::cp_heat::init();

    #[cfg(feature = "cp_weight")]
    crate::cp_weight::init();

    log!("init finish");

    handler_task::start();
    status_task::start();

    crate::conn::can_conn::recv_loop();
}
















